package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.exception.XueChengPlusException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    private TeachplanMapper teachplanMapper;
    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    //新增和修改课程计划--章
    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplan) {
        Long id = teachplan.getId();
        if(id==null){
            int count=getTeachplanCount(teachplan.getCourseId(),teachplan.getParentid());
            Teachplan teachplanNew = new Teachplan();
            BeanUtils.copyProperties(teachplan,teachplanNew);
            teachplanNew.setCreateDate(LocalDateTime.now());
            teachplanNew.setOrderby(count+1);
            teachplanMapper.insert(teachplanNew);
        }else{
            Teachplan teachplanNew = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplan,teachplanNew);
            teachplanNew.setChangeDate(LocalDateTime.now());
            teachplanMapper.updateById(teachplanNew);
        }
    }
    //删除课程计划
    @Override
    @Transactional
    public void deleteTeachplan(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        if(teachplan.getParentid().equals(0)){
            List<TeachplanDto> list=teachplanMapper.selectByParentId(id);
            if(list!=null && list.size()>0){
                throw new XueChengPlusException("课程计划信息还有子级信息，无法操作");
            }else{
                teachplanMapper.deleteById(id);
            }
        }else{
            teachplanMediaMapper.deleteByTeahplanId(id);
            teachplanMapper.deleteById(id);
        }
    }

    //移动章节或者小节
    @Override
    @Transactional
    public void moveTeachPlan(String move, Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        int count = getTeachplanCount(teachplan.getCourseId(), teachplan.getParentid());

        if(move.equals("moveup")){
            Teachplan teachplan1 = teachplanMapper.selectById(id - 1);
            if(teachplan1==null){
                throw new XueChengPlusException("章节已在顶部，无需移动");
            }
            teachplan1.setOrderby(count);
            teachplanMapper.updateById(teachplan1);
            teachplan.setOrderby(count-1);
            teachplanMapper.updateById(teachplan);

        }else if(move.equals("movedown")){
            Teachplan teachplan1 = teachplanMapper.selectById(id + 1);
            if(teachplan1==null){
                throw new XueChengPlusException("章节已在底部，无需移动");
            }
            teachplan1.setOrderby(count);
            teachplanMapper.updateById(teachplan1);
            teachplan.setOrderby(count+1);
            teachplanMapper.updateById(teachplan);
        }
    }

    //教学计划绑定媒资
    @Override
    @Transactional
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            XueChengPlusException.cast("教学计划不存在");
        }
        if(teachplan.getGrade()!=2){
            XueChengPlusException.cast("不是小章节，无法绑定媒资计划");
        }
        Long courseId = teachplan.getCourseId();

        // 先删除原本该教学计划的绑定媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId,teachplanId));

        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);


        return teachplanMedia;
    }

    //删除教学媒资计划
    @Override
    public void deleteTeachplanMedia(String teachPlanId, String mediaId) {
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getMediaId,mediaId));
    }

    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }
}
